package com.katze.boot.applet.asn1.adapter;

import com.katze.boot.applet.asn1.anlysis.BinaryAnalysis;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class BinaryCommonAdapter implements BasicAdapter {
    private static BinaryCommonAdapter adapter = null;
    private Path file;
    private Path output;
    private String filename;
    private int length;

    public BinaryCommonAdapter() {}

    private void reset(Path file, String filename, int length, Path output) {
        this.file = file;
        this.filename = filename;
        this.length = length;
        this.output = output;
    }

    public static BinaryCommonAdapter getInstance(Path file, String filename, int length, Path output){
        if (adapter == null) adapter = new BinaryCommonAdapter();
        adapter.reset(file, filename, length, output);
        return adapter;
    }

    @Override
    public void run() {
        try (BinaryAnalysis analysis = new BinaryAnalysis();
             InputStream inputStream = Files.newInputStream(file)) {
            analysis.config(filename).storage(output);
            byte[] bs = new byte[length];//该话单为固定块大小
            while ((inputStream.read(bs)) >=0 ){
                analysis.save(bs);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
